package com.example.supplychainbackend.entity.design;

import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicInteger;

@Entity
@Table(name = "bom")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BOMEntity {

    String	bomLineNumber;

    String orderId;

    @Id
    String	materialCode;
    String	materialName;
    String	materialDesc;

    Integer	quantity;
    String	unit;
    String	manufacturer;
    Integer	materialCategory;
    Integer	materialSubCategory;
    String	materialSource;
    String	mrpType;
    String	leadTime;
    String	oldMaterialCode;
    String	customerMaterialCode;
    String	manufacturerMaterialCode;
    String	componentScrapRate;
    String	operationScrapRate;
    String	isInvalidAssemblyScrap;
    String	specialProcurementType;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @Transient
    private static final AtomicInteger counter1 = new AtomicInteger(1);
    @Transient
    private static final AtomicInteger counter2 = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (bomLineNumber == null || bomLineNumber.equals("")) {
            bomLineNumber = String.format("%04d.%03d0",
                    counter1.get()/100, counter1.getAndIncrement());
        }
        if (materialCode == null || materialCode.equals("")) {
            materialCode = String.format("MAT-%03d0", counter2.getAndIncrement());
        }
    }
}
